﻿using System;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace LongSpeed.WebSite.Behaviors
{
    /// <summary>
    /// T_UrlMapping:实体类(属性说明自动提取数据库字段的描述信息)
    /// </summary>
    [Serializable]
    public partial class UrlMapping
    {
        public UrlMapping()
        { }

        #region Model
        private int _id;
        private string _toaddress;
        private string _formaddress;
        private string _args;
        /// <summary>
        /// 
        /// </summary>
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 真实处理页面地址
        /// </summary>
        public string ToAddress
        {
            set { _toaddress = value; }
            get { return _toaddress; }
        }
        /// <summary>
        /// 请求处理页面地址
        /// </summary>
        public string FormAddress
        {
            set { _formaddress = value; }
            get { return _formaddress; }
        }
        /// <summary>
        /// 需要的参数
        /// </summary>
        public string Args
        {
            set { _args = value; }
            get { return _args; }
        }
        #endregion Model

    }
    /// <summary>
    /// 数据访问类:T_UrlMapping
    /// </summary>
    public partial class UrlMapping
    {
        public static System.Collections.Generic.Dictionary<string, UrlMapping> Cache = new System.Collections.Generic.Dictionary<string, UrlMapping>();

        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return OleDbHelper.GetMaxID("ID", "T_UrlMapping");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_UrlMapping");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = ID;

            return OleDbHelper.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string formAddress)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_UrlMapping");
            strSql.Append(" where FormAddress=@FormAddress");
            OleDbParameter[] parameters = {
					new OleDbParameter("@FormAddress", OleDbType.VarChar,255)
			};
            parameters[0].Value = formAddress;

            return OleDbHelper.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(UrlMapping model)
        {
            object fromUlr;
            if (string.IsNullOrWhiteSpace(model.FormAddress) || string.IsNullOrEmpty(model.FormAddress) || model.FormAddress == "") fromUlr = DBNull.Value;
            else fromUlr = model.FormAddress;

            if (fromUlr != DBNull.Value)
            {
                var urlM = this.GetModel(model.ID);
                if (urlM != null && urlM.FormAddress != model.FormAddress && this.Exists(model.FormAddress))
                {
                    var ex = new Exception(string.Format("已存在名称为\"{0}\"的Url地址，请更换！", model.FormAddress));
                    throw ex;
                }
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_UrlMapping(");
            strSql.Append("ToAddress,FormAddress,Args)");
            strSql.Append(" values (");
            strSql.Append("@ToAddress,@FormAddress,@Args)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ToAddress", OleDbType.VarChar,255),
					new OleDbParameter("@FormAddress", OleDbType.VarChar,255),
					new OleDbParameter("@Args", OleDbType.VarChar,255)};
            parameters[0].Value = model.ToAddress;
            parameters[1].Value = fromUlr;
            parameters[2].Value = model.Args;

            int rows = OleDbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                model.ID = GetMaxId();
                if (!Cache.ContainsKey(model.FormAddress))
                {
                    Cache[model.FormAddress] = model;
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(UrlMapping model)
        {
            if (model.ID == 0)
            {
                return Add(model);
            }

            object fromUlr;
            if (string.IsNullOrWhiteSpace(model.FormAddress) || string.IsNullOrEmpty(model.FormAddress)) fromUlr = DBNull.Value;
            else fromUlr = model.FormAddress;

            if (fromUlr != DBNull.Value)
            {
                var urlM = this.GetModel(model.ID);
                if (urlM != null && urlM.FormAddress != model.FormAddress && this.Exists(model.FormAddress))
                {
                    throw new Exception(string.Format("已存在名称为\"{0}\"的Url地址，请更换！", model.FormAddress));
                }
            }


            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_UrlMapping set ");
            strSql.Append("ToAddress=@ToAddress,");
            strSql.Append("FormAddress=@FormAddress,");
            strSql.Append("Args=@Args");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ToAddress", OleDbType.VarChar,255),
					new OleDbParameter("@FormAddress", OleDbType.VarChar,255),
					new OleDbParameter("@Args", OleDbType.VarChar,255),
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = model.ToAddress;
            parameters[1].Value = fromUlr;
            parameters[2].Value = model.Args;
            parameters[3].Value = model.ID;

            int rows = OleDbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                if (Cache.ContainsKey(model.FormAddress))
                {
                    Cache[model.FormAddress] = model;
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_UrlMapping ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = ID;

            int rows = OleDbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string formAddress)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_UrlMapping ");
            strSql.Append(" where FormAddress=@FormAddress");
            OleDbParameter[] parameters = {
					new OleDbParameter("@FormAddress", OleDbType.VarChar,255)
			};
            parameters[0].Value = formAddress;

            int rows = OleDbHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                if (Cache.ContainsKey(formAddress))
                {
                    Cache.Remove(formAddress);
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_UrlMapping ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = OleDbHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UrlMapping GetModel(int ID)
        {
            var cache = Cache.Values.Where(o => o.ID == ID).SingleOrDefault();
            if (cache != null)
            {
                return cache;
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ToAddress,FormAddress,Args from T_UrlMapping ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)
			};
            parameters[0].Value = ID;

            UrlMapping model = new UrlMapping();
            DataSet ds = OleDbHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ToAddress"] != null && ds.Tables[0].Rows[0]["ToAddress"].ToString() != "")
                {
                    model.ToAddress = ds.Tables[0].Rows[0]["ToAddress"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FormAddress"] != null && ds.Tables[0].Rows[0]["FormAddress"].ToString() != "")
                {
                    model.FormAddress = ds.Tables[0].Rows[0]["FormAddress"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Args"] != null && ds.Tables[0].Rows[0]["Args"].ToString() != "")
                {
                    model.Args = ds.Tables[0].Rows[0]["Args"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UrlMapping GetModel(string formAddress)
        {
            if (Cache.ContainsKey(formAddress))
            {
                return Cache[formAddress];
            }

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ToAddress,FormAddress,Args from T_UrlMapping ");
            strSql.Append(" where FormAddress=@FormAddress");
            OleDbParameter[] parameters = {
					new OleDbParameter("@FormAddress", OleDbType.VarChar,255)
			};
            parameters[0].Value = formAddress;

            UrlMapping model = new UrlMapping();
            DataSet ds = OleDbHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ToAddress"] != null && ds.Tables[0].Rows[0]["ToAddress"].ToString() != "")
                {
                    model.ToAddress = ds.Tables[0].Rows[0]["ToAddress"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FormAddress"] != null && ds.Tables[0].Rows[0]["FormAddress"].ToString() != "")
                {
                    model.FormAddress = ds.Tables[0].Rows[0]["FormAddress"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Args"] != null && ds.Tables[0].Rows[0]["Args"].ToString() != "")
                {
                    model.Args = ds.Tables[0].Rows[0]["Args"].ToString();
                }

                Cache[formAddress] = model;

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ToAddress,FormAddress,Args ");
            strSql.Append(" FROM T_UrlMapping ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return OleDbHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_UrlMapping ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = OleDbHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from T_UrlMapping T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return OleDbHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "T_UrlMapping";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return OleDbHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}